﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using _1.ListAllDirectories.ServiceReferenceListAllDirectories;

namespace _1.ListAllDirectories
{
    public partial class MainPage : UserControl
    {
        private string back;

        public MainPage()
        {
            InitializeComponent();
        }

        private void buttonList_Click(object sender, RoutedEventArgs e)
        {
            ListDirsAndFiles(textBoxPath.Text);
        }

        void client_ListFilesCompleted(object sender, ListFilesCompletedEventArgs e)
        {
            TextBlock newTextBlockFiles = new TextBlock();
            newTextBlockFiles.Text = "Files:";
            StackPanelDirs.Children.Add(newTextBlockFiles);

            foreach (var item in e.Result)
            {
                TextBlock newTextBlock = new TextBlock();
                newTextBlock.Text = item;
                StackPanelDirs.Children.Add(newTextBlock);
            }
        }

        void client_ListDirsCompleted(object sender, ListDirsCompletedEventArgs e)
        {
            StackPanelDirs.Children.Clear();
            foreach (var item in e.Result)
            {
                HyperlinkButton newHypBytton = new HyperlinkButton();
                newHypBytton.Content = item;
                newHypBytton.Click += newHypBytton_Click;
                StackPanelDirs.Children.Add(newHypBytton);    
            }
        }

        void newHypBytton_Click(object sender, RoutedEventArgs e)
        {
            buttonBack.IsEnabled = true;
            HyperlinkButton hyperlinkButton = sender as HyperlinkButton;
            string path = hyperlinkButton.Content.ToString();
            ListDirsAndFiles(path);
            textBoxPath.Text = path.Substring(path.LastIndexOf('\\') + 1);
            this.back = path.Substring(0, path.LastIndexOf('\\'));
        }

        private void ListDirsAndFiles(string path)
        {
            ServiceGetDirsClient client = new ServiceGetDirsClient();
            client.ListDirsCompleted += client_ListDirsCompleted;
            client.ListDirsAsync(path);            
            client.ListFilesCompleted += client_ListFilesCompleted;
            client.ListFilesAsync(path);
        }

        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {            
            ListDirsAndFiles(back);

            if (back.IndexOf('\\') > 0)
            {
                textBoxPath.Text = back.Substring(back.LastIndexOf('\\') + 1);
                this.back = back.Substring(0, back.LastIndexOf('\\'));
            }
            else
            {
                ListDirsAndFiles("c:\\");
                textBoxPath.Text = "c:\\";
                buttonBack.IsEnabled = false;
            }
        }
    }
}
